1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module glib.MainContext;
26 
27 private import glib.Cond;
28 private import glib.ConstructionException;
29 private import glib.Mutex;
30 private import glib.Source;
31 private import glib.c.functions;
32 public  import glib.c.types;
33 private import linker.Loader;
34 
35 
36 /**
37  * The `GMainContext` struct is an opaque data
38  * type representing a set of sources to be handled in a main loop.
39  */
40 public class MainContext
41 {
42 	/** the main Gtk struct */
43 	protected GMainContext* gMainContext;
44 	protected bool ownedRef;
45 
46 	/** Get the main Gtk struct */
47 	public GMainContext* getMainContextStruct(bool transferOwnership = false)
48 	{
49 		if (transferOwnership)
50 			ownedRef = false;
51 		return gMainContext;
52 	}
53 
54 	/** the main Gtk struct as a void* */
55 	protected void* getStruct()
56 	{
57 		return cast(void*)gMainContext;
58 	}
59 
60 	/**
61 	 * Sets our main struct and passes it to the parent class.
62 	 */
63 	public this (GMainContext* gMainContext, bool ownedRef = false)
64 	{
65 		this.gMainContext = gMainContext;
66 		this.ownedRef = ownedRef;
67 	}
68 
69 	~this ()
70 	{
71 		if ( Linker.isLoaded(LIBRARY_GLIB[0]) && ownedRef )
72 			g_main_context_unref(gMainContext);
73 	}
74 
75 
76 	/**
77 	 * Creates a new #GMainContext structure.
78 	 *
79 	 * Returns: the new #GMainContext
80 	 *
81 	 * Throws: ConstructionException GTK+ fails to create the object.
82 	 */
83 	public this()
84 	{
85 		auto __p = g_main_context_new();
86 
87 		if(__p is null)
88 		{
89 			throw new ConstructionException("null returned by new");
90 		}
91 
92 		this(cast(GMainContext*) __p);
93 	}
94 
95 	/**
96 	 * Creates a new #GMainContext structure.
97 	 *
98 	 * Params:
99 	 *     flags = a bitwise-OR combination of #GMainContextFlags flags that can only be
100 	 *         set at creation time.
101 	 *
102 	 * Returns: the new #GMainContext
103 	 *
104 	 * Since: 2.72
105 	 *
106 	 * Throws: ConstructionException GTK+ fails to create the object.
107 	 */
108 	public this(GMainContextFlags flags)
109 	{
110 		auto __p = g_main_context_new_with_flags(flags);
111 
112 		if(__p is null)
113 		{
114 			throw new ConstructionException("null returned by new_with_flags");
115 		}
116 
117 		this(cast(GMainContext*) __p);
118 	}
119 
120 	/**
121 	 * Tries to become the owner of the specified context.
122 	 * If some other thread is the owner of the context,
123 	 * returns %FALSE immediately. Ownership is properly
124 	 * recursive: the owner can require ownership again
125 	 * and will release ownership when g_main_context_release()
126 	 * is called as many times as g_main_context_acquire().
127 	 *
128 	 * You must be the owner of a context before you
129 	 * can call g_main_context_prepare(), g_main_context_query(),
130 	 * g_main_context_check(), g_main_context_dispatch().
131 	 *
132 	 * Returns: %TRUE if the operation succeeded, and
133 	 *     this thread is now the owner of @context.
134 	 */
135 	public bool acquire()
136 	{
137 		return g_main_context_acquire(gMainContext) != 0;
138 	}
139 
140 	/**
141 	 * Adds a file descriptor to the set of file descriptors polled for
142 	 * this context. This will very seldom be used directly. Instead
143 	 * a typical event source will use g_source_add_unix_fd() instead.
144 	 *
145 	 * Params:
146 	 *     fd = a #GPollFD structure holding information about a file
147 	 *         descriptor to watch.
148 	 *     priority = the priority for this file descriptor which should be
149 	 *         the same as the priority used for g_source_attach() to ensure that the
150 	 *         file descriptor is polled whenever the results may be needed.
151 	 */
152 	public void addPoll(GPollFD* fd, int priority)
153 	{
154 		g_main_context_add_poll(gMainContext, fd, priority);
155 	}
156 
157 	/**
158 	 * Passes the results of polling back to the main loop. You should be
159 	 * careful to pass @fds and its length @n_fds as received from
160 	 * g_main_context_query(), as this functions relies on assumptions
161 	 * on how @fds is filled.
162 	 *
163 	 * You must have successfully acquired the context with
164 	 * g_main_context_acquire() before you may call this function.
165 	 *
166 	 * Params:
167 	 *     maxPriority = the maximum numerical priority of sources to check
168 	 *     fds = array of #GPollFD's that was passed to
169 	 *         the last call to g_main_context_query()
170 	 *
171 	 * Returns: %TRUE if some sources are ready to be dispatched.
172 	 */
173 	public bool check(int maxPriority, GPollFD[] fds)
174 	{
175 		return g_main_context_check(gMainContext, maxPriority, fds.ptr, cast(int)fds.length) != 0;
176 	}
177 
178 	/**
179 	 * Dispatches all pending sources.
180 	 *
181 	 * You must have successfully acquired the context with
182 	 * g_main_context_acquire() before you may call this function.
183 	 */
184 	public void dispatch()
185 	{
186 		g_main_context_dispatch(gMainContext);
187 	}
188 
189 	/**
190 	 * Finds a source with the given source functions and user data.  If
191 	 * multiple sources exist with the same source function and user data,
192 	 * the first one found will be returned.
193 	 *
194 	 * Params:
195 	 *     funcs = the @source_funcs passed to g_source_new().
196 	 *     userData = the user data from the callback.
197 	 *
198 	 * Returns: the source, if one was found, otherwise %NULL
199 	 */
200 	public Source findSourceByFuncsUserData(GSourceFuncs* funcs, void* userData)
201 	{
202 		auto __p = g_main_context_find_source_by_funcs_user_data(gMainContext, funcs, userData);
203 
204 		if(__p is null)
205 		{
206 			return null;
207 		}
208 
209 		return new Source(cast(GSource*) __p);
210 	}
211 
212 	/**
213 	 * Finds a #GSource given a pair of context and ID.
214 	 *
215 	 * It is a programmer error to attempt to look up a non-existent source.
216 	 *
217 	 * More specifically: source IDs can be reissued after a source has been
218 	 * destroyed and therefore it is never valid to use this function with a
219 	 * source ID which may have already been removed.  An example is when
220 	 * scheduling an idle to run in another thread with g_idle_add(): the
221 	 * idle may already have run and been removed by the time this function
222 	 * is called on its (now invalid) source ID.  This source ID may have
223 	 * been reissued, leading to the operation being performed against the
224 	 * wrong source.
225 	 *
226 	 * Params:
227 	 *     sourceId = the source ID, as returned by g_source_get_id().
228 	 *
229 	 * Returns: the #GSource
230 	 */
231 	public Source findSourceById(uint sourceId)
232 	{
233 		auto __p = g_main_context_find_source_by_id(gMainContext, sourceId);
234 
235 		if(__p is null)
236 		{
237 			return null;
238 		}
239 
240 		return new Source(cast(GSource*) __p);
241 	}
242 
243 	/**
244 	 * Finds a source with the given user data for the callback.  If
245 	 * multiple sources exist with the same user data, the first
246 	 * one found will be returned.
247 	 *
248 	 * Params:
249 	 *     userData = the user_data for the callback.
250 	 *
251 	 * Returns: the source, if one was found, otherwise %NULL
252 	 */
253 	public Source findSourceByUserData(void* userData)
254 	{
255 		auto __p = g_main_context_find_source_by_user_data(gMainContext, userData);
256 
257 		if(__p is null)
258 		{
259 			return null;
260 		}
261 
262 		return new Source(cast(GSource*) __p);
263 	}
264 
265 	/**
266 	 * Gets the poll function set by g_main_context_set_poll_func().
267 	 *
268 	 * Returns: the poll function
269 	 */
270 	public GPollFunc getPollFunc()
271 	{
272 		return g_main_context_get_poll_func(gMainContext);
273 	}
274 
275 	/**
276 	 * Invokes a function in such a way that @context is owned during the
277 	 * invocation of @function.
278 	 *
279 	 * If @context is %NULL then the global default main context — as
280 	 * returned by g_main_context_default() — is used.
281 	 *
282 	 * If @context is owned by the current thread, @function is called
283 	 * directly.  Otherwise, if @context is the thread-default main context
284 	 * of the current thread and g_main_context_acquire() succeeds, then
285 	 * @function is called and g_main_context_release() is called
286 	 * afterwards.
287 	 *
288 	 * In any other case, an idle source is created to call @function and
289 	 * that source is attached to @context (presumably to be run in another
290 	 * thread).  The idle source is attached with %G_PRIORITY_DEFAULT
291 	 * priority.  If you want a different priority, use
292 	 * g_main_context_invoke_full().
293 	 *
294 	 * Note that, as with normal idle functions, @function should probably
295 	 * return %FALSE.  If it returns %TRUE, it will be continuously run in a
296 	 * loop (and may prevent this call from returning).
297 	 *
298 	 * Params:
299 	 *     function_ = function to call
300 	 *     data = data to pass to @function
301 	 *
302 	 * Since: 2.28
303 	 */
304 	public void invoke(GSourceFunc function_, void* data)
305 	{
306 		g_main_context_invoke(gMainContext, function_, data);
307 	}
308 
309 	/**
310 	 * Invokes a function in such a way that @context is owned during the
311 	 * invocation of @function.
312 	 *
313 	 * This function is the same as g_main_context_invoke() except that it
314 	 * lets you specify the priority in case @function ends up being
315 	 * scheduled as an idle and also lets you give a #GDestroyNotify for @data.
316 	 *
317 	 * @notify should not assume that it is called from any particular
318 	 * thread or with any particular context acquired.
319 	 *
320 	 * Params:
321 	 *     priority = the priority at which to run @function
322 	 *     function_ = function to call
323 	 *     data = data to pass to @function
324 	 *     notify = a function to call when @data is no longer in use, or %NULL.
325 	 *
326 	 * Since: 2.28
327 	 */
328 	public void invokeFull(int priority, GSourceFunc function_, void* data, GDestroyNotify notify)
329 	{
330 		g_main_context_invoke_full(gMainContext, priority, function_, data, notify);
331 	}
332 
333 	/**
334 	 * Determines whether this thread holds the (recursive)
335 	 * ownership of this #GMainContext. This is useful to
336 	 * know before waiting on another thread that may be
337 	 * blocking to get ownership of @context.
338 	 *
339 	 * Returns: %TRUE if current thread is owner of @context.
340 	 *
341 	 * Since: 2.10
342 	 */
343 	public bool isOwner()
344 	{
345 		return g_main_context_is_owner(gMainContext) != 0;
346 	}
347 
348 	/**
349 	 * Runs a single iteration for the given main loop. This involves
350 	 * checking to see if any event sources are ready to be processed,
351 	 * then if no events sources are ready and @may_block is %TRUE, waiting
352 	 * for a source to become ready, then dispatching the highest priority
353 	 * events sources that are ready. Otherwise, if @may_block is %FALSE
354 	 * sources are not waited to become ready, only those highest priority
355 	 * events sources will be dispatched (if any), that are ready at this
356 	 * given moment without further waiting.
357 	 *
358 	 * Note that even when @may_block is %TRUE, it is still possible for
359 	 * g_main_context_iteration() to return %FALSE, since the wait may
360 	 * be interrupted for other reasons than an event source becoming ready.
361 	 *
362 	 * Params:
363 	 *     mayBlock = whether the call may block.
364 	 *
365 	 * Returns: %TRUE if events were dispatched.
366 	 */
367 	public bool iteration(bool mayBlock)
368 	{
369 		return g_main_context_iteration(gMainContext, mayBlock) != 0;
370 	}
371 
372 	/**
373 	 * Checks if any sources have pending events for the given context.
374 	 *
375 	 * Returns: %TRUE if events are pending.
376 	 */
377 	public bool pending()
378 	{
379 		return g_main_context_pending(gMainContext) != 0;
380 	}
381 
382 	/**
383 	 * Pops @context off the thread-default context stack (verifying that
384 	 * it was on the top of the stack).
385 	 *
386 	 * Since: 2.22
387 	 */
388 	public void popThreadDefault()
389 	{
390 		g_main_context_pop_thread_default(gMainContext);
391 	}
392 
393 	/**
394 	 * Prepares to poll sources within a main loop. The resulting information
395 	 * for polling is determined by calling g_main_context_query ().
396 	 *
397 	 * You must have successfully acquired the context with
398 	 * g_main_context_acquire() before you may call this function.
399 	 *
400 	 * Params:
401 	 *     priority = location to store priority of highest priority
402 	 *         source already ready.
403 	 *
404 	 * Returns: %TRUE if some source is ready to be dispatched
405 	 *     prior to polling.
406 	 */
407 	public bool prepare(out int priority)
408 	{
409 		return g_main_context_prepare(gMainContext, &priority) != 0;
410 	}
411 
412 	/**
413 	 * Acquires @context and sets it as the thread-default context for the
414 	 * current thread. This will cause certain asynchronous operations
415 	 * (such as most [gio][gio]-based I/O) which are
416 	 * started in this thread to run under @context and deliver their
417 	 * results to its main loop, rather than running under the global
418 	 * default context in the main thread. Note that calling this function
419 	 * changes the context returned by g_main_context_get_thread_default(),
420 	 * not the one returned by g_main_context_default(), so it does not affect
421 	 * the context used by functions like g_idle_add().
422 	 *
423 	 * Normally you would call this function shortly after creating a new
424 	 * thread, passing it a #GMainContext which will be run by a
425 	 * #GMainLoop in that thread, to set a new default context for all
426 	 * async operations in that thread. In this case you may not need to
427 	 * ever call g_main_context_pop_thread_default(), assuming you want the
428 	 * new #GMainContext to be the default for the whole lifecycle of the
429 	 * thread.
430 	 *
431 	 * If you don't have control over how the new thread was created (e.g.
432 	 * in the new thread isn't newly created, or if the thread life
433 	 * cycle is managed by a #GThreadPool), it is always suggested to wrap
434 	 * the logic that needs to use the new #GMainContext inside a
435 	 * g_main_context_push_thread_default() / g_main_context_pop_thread_default()
436 	 * pair, otherwise threads that are re-used will end up never explicitly
437 	 * releasing the #GMainContext reference they hold.
438 	 *
439 	 * In some cases you may want to schedule a single operation in a
440 	 * non-default context, or temporarily use a non-default context in
441 	 * the main thread. In that case, you can wrap the call to the
442 	 * asynchronous operation inside a
443 	 * g_main_context_push_thread_default() /
444 	 * g_main_context_pop_thread_default() pair, but it is up to you to
445 	 * ensure that no other asynchronous operations accidentally get
446 	 * started while the non-default context is active.
447 	 *
448 	 * Beware that libraries that predate this function may not correctly
449 	 * handle being used from a thread with a thread-default context. Eg,
450 	 * see g_file_supports_thread_contexts().
451 	 *
452 	 * Since: 2.22
453 	 */
454 	public void pushThreadDefault()
455 	{
456 		g_main_context_push_thread_default(gMainContext);
457 	}
458 
459 	/**
460 	 * Determines information necessary to poll this main loop. You should
461 	 * be careful to pass the resulting @fds array and its length @n_fds
462 	 * as is when calling g_main_context_check(), as this function relies
463 	 * on assumptions made when the array is filled.
464 	 *
465 	 * You must have successfully acquired the context with
466 	 * g_main_context_acquire() before you may call this function.
467 	 *
468 	 * Params:
469 	 *     maxPriority = maximum priority source to check
470 	 *     timeout = location to store timeout to be used in polling
471 	 *     fds = location to
472 	 *         store #GPollFD records that need to be polled.
473 	 *
474 	 * Returns: the number of records actually stored in @fds,
475 	 *     or, if more than @n_fds records need to be stored, the number
476 	 *     of records that need to be stored.
477 	 */
478 	public int query(int maxPriority, out int timeout, GPollFD[] fds)
479 	{
480 		return g_main_context_query(gMainContext, maxPriority, &timeout, fds.ptr, cast(int)fds.length);
481 	}
482 
483 	alias doref = ref_;
484 	/**
485 	 * Increases the reference count on a #GMainContext object by one.
486 	 *
487 	 * Returns: the @context that was passed in (since 2.6)
488 	 */
489 	public MainContext ref_()
490 	{
491 		auto __p = g_main_context_ref(gMainContext);
492 
493 		if(__p is null)
494 		{
495 			return null;
496 		}
497 
498 		return new MainContext(cast(GMainContext*) __p, true);
499 	}
500 
501 	/**
502 	 * Releases ownership of a context previously acquired by this thread
503 	 * with g_main_context_acquire(). If the context was acquired multiple
504 	 * times, the ownership will be released only when g_main_context_release()
505 	 * is called as many times as it was acquired.
506 	 */
507 	public void release()
508 	{
509 		g_main_context_release(gMainContext);
510 	}
511 
512 	/**
513 	 * Removes file descriptor from the set of file descriptors to be
514 	 * polled for a particular context.
515 	 *
516 	 * Params:
517 	 *     fd = a #GPollFD descriptor previously added with g_main_context_add_poll()
518 	 */
519 	public void removePoll(GPollFD* fd)
520 	{
521 		g_main_context_remove_poll(gMainContext, fd);
522 	}
523 
524 	/**
525 	 * Sets the function to use to handle polling of file descriptors. It
526 	 * will be used instead of the poll() system call
527 	 * (or GLib's replacement function, which is used where
528 	 * poll() isn't available).
529 	 *
530 	 * This function could possibly be used to integrate the GLib event
531 	 * loop with an external event loop.
532 	 *
533 	 * Params:
534 	 *     func = the function to call to poll all file descriptors
535 	 */
536 	public void setPollFunc(GPollFunc func)
537 	{
538 		g_main_context_set_poll_func(gMainContext, func);
539 	}
540 
541 	/**
542 	 * Decreases the reference count on a #GMainContext object by one. If
543 	 * the result is zero, free the context and free all associated memory.
544 	 */
545 	public void unref()
546 	{
547 		g_main_context_unref(gMainContext);
548 	}
549 
550 	/**
551 	 * Tries to become the owner of the specified context,
552 	 * as with g_main_context_acquire(). But if another thread
553 	 * is the owner, atomically drop @mutex and wait on @cond until
554 	 * that owner releases ownership or until @cond is signaled, then
555 	 * try again (once) to become the owner.
556 	 *
557 	 * Deprecated: Use g_main_context_is_owner() and separate locking instead.
558 	 *
559 	 * Params:
560 	 *     cond = a condition variable
561 	 *     mutex = a mutex, currently held
562 	 *
563 	 * Returns: %TRUE if the operation succeeded, and
564 	 *     this thread is now the owner of @context.
565 	 */
566 	public bool wait(Cond cond, Mutex mutex)
567 	{
568 		return g_main_context_wait(gMainContext, (cond is null) ? null : cond.getCondStruct(), (mutex is null) ? null : mutex.getMutexStruct()) != 0;
569 	}
570 
571 	/**
572 	 * If @context is currently blocking in g_main_context_iteration()
573 	 * waiting for a source to become ready, cause it to stop blocking
574 	 * and return.  Otherwise, cause the next invocation of
575 	 * g_main_context_iteration() to return without blocking.
576 	 *
577 	 * This API is useful for low-level control over #GMainContext; for
578 	 * example, integrating it with main loop implementations such as
579 	 * #GMainLoop.
580 	 *
581 	 * Another related use for this function is when implementing a main
582 	 * loop with a termination condition, computed from multiple threads:
583 	 *
584 	 * |[<!-- language="C" -->
585 	 * #define NUM_TASKS 10
586 	 * static gint tasks_remaining = NUM_TASKS;  // (atomic)
587 	 * ...
588 	 *
589 	 * while (g_atomic_int_get (&tasks_remaining) != 0)
590 	 * g_main_context_iteration (NULL, TRUE);
591 	 * ]|
592 	 *
593 	 * Then in a thread:
594 	 * |[<!-- language="C" -->
595 	 * perform_work();
596 	 *
597 	 * if (g_atomic_int_dec_and_test (&tasks_remaining))
598 	 * g_main_context_wakeup (NULL);
599 	 * ]|
600 	 */
601 	public void wakeup()
602 	{
603 		g_main_context_wakeup(gMainContext);
604 	}
605 
606 	/**
607 	 * Returns the global default main context. This is the main context
608 	 * used for main loop functions when a main loop is not explicitly
609 	 * specified, and corresponds to the "main" main loop. See also
610 	 * g_main_context_get_thread_default().
611 	 *
612 	 * Returns: the global default main context.
613 	 */
614 	public static MainContext default_()
615 	{
616 		auto __p = g_main_context_default();
617 
618 		if(__p is null)
619 		{
620 			return null;
621 		}
622 
623 		return new MainContext(cast(GMainContext*) __p);
624 	}
625 
626 	/**
627 	 * Gets the thread-default #GMainContext for this thread. Asynchronous
628 	 * operations that want to be able to be run in contexts other than
629 	 * the default one should call this method or
630 	 * g_main_context_ref_thread_default() to get a #GMainContext to add
631 	 * their #GSources to. (Note that even in single-threaded
632 	 * programs applications may sometimes want to temporarily push a
633 	 * non-default context, so it is not safe to assume that this will
634 	 * always return %NULL if you are running in the default thread.)
635 	 *
636 	 * If you need to hold a reference on the context, use
637 	 * g_main_context_ref_thread_default() instead.
638 	 *
639 	 * Returns: the thread-default #GMainContext, or
640 	 *     %NULL if the thread-default context is the global default context.
641 	 *
642 	 * Since: 2.22
643 	 */
644 	public static MainContext getThreadDefault()
645 	{
646 		auto __p = g_main_context_get_thread_default();
647 
648 		if(__p is null)
649 		{
650 			return null;
651 		}
652 
653 		return new MainContext(cast(GMainContext*) __p);
654 	}
655 
656 	/**
657 	 * Gets the thread-default #GMainContext for this thread, as with
658 	 * g_main_context_get_thread_default(), but also adds a reference to
659 	 * it with g_main_context_ref(). In addition, unlike
660 	 * g_main_context_get_thread_default(), if the thread-default context
661 	 * is the global default context, this will return that #GMainContext
662 	 * (with a ref added to it) rather than returning %NULL.
663 	 *
664 	 * Returns: the thread-default #GMainContext. Unref
665 	 *     with g_main_context_unref() when you are done with it.
666 	 *
667 	 * Since: 2.32
668 	 */
669 	public static MainContext refThreadDefault()
670 	{
671 		auto __p = g_main_context_ref_thread_default();
672 
673 		if(__p is null)
674 		{
675 			return null;
676 		}
677 
678 		return new MainContext(cast(GMainContext*) __p, true);
679 	}
680 }